package com.sunnybear.framework.ui.recyclerview;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.caimuhao.rxpicker.utils.DensityUtil;
import com.sunnybear.framework.R;
import com.sunnybear.framework.library.base.Presenter;
import com.sunnybear.framework.tools.GlobalHelper;

import java.util.List;

import androidx.databinding.ViewDataBinding;

/**
 * <p>
 *  2018/7/28.
 */
public abstract class CaseNoDataBaseAdapter<Item, VH extends BaseViewHolder> extends BaseAdapter<Item, VH> {
    /**
     * 全局缓存的支持无数据item展示的RecycleView高度
     */
    public static final String GLOBAL_CASE_NODATA_RECYCLEVIEW_HEIGHT = "global_case_nodata_recycleview_height";
    /**
     * 无数据的item
     */
    protected static final int NO_DATA_ITEM_VIEW_TYPE = 100;

    private static final Byte[] LOCK = new Byte[0];

    public CaseNoDataBaseAdapter(List<Item> items) {
        super(items);
    }

    public CaseNoDataBaseAdapter(List<Item> items, Presenter presenter) {
        super(items, presenter);

    }

    @Override
    public int getItemCount() {
        synchronized (LOCK) {
            if (mItems.size() == 0) {
                mItems.add(null);
            }
        }
        return super.getItemCount();
    }


    @Override
    public void onBindViewHolder(VH holder, int position) {
        super.onBindViewHolder(holder, position);
        if (getItemViewType(position) == NO_DATA_ITEM_VIEW_TYPE) {
            int height = GlobalHelper.getInstance().getValue(GLOBAL_CASE_NODATA_RECYCLEVIEW_HEIGHT, DensityUtil.getScreenH(mContext));
            holder.getViewDataBinding().getRoot().getLayoutParams().height = height;
        }
    }

    @Override
    protected int getItemLayoutId(int viewType) {
        if (viewType == NO_DATA_ITEM_VIEW_TYPE) {
            return getNoDataItemLayoutId();
        }
        return getNormalLayoutId(viewType);
    }


    protected abstract int getNormalLayoutId(int viewType);

    @Override
    protected VH getViewHolder(int viewType, ViewDataBinding viewDataBinding) {
        if (viewType == NO_DATA_ITEM_VIEW_TYPE) {
            return getNoDataViewHolder(viewType, viewDataBinding);
        }
        return getNormalViewHolder(viewType, viewDataBinding);
    }

    protected abstract VH getNormalViewHolder(int viewType, ViewDataBinding viewDataBinding);

    protected abstract VH getNoDataViewHolder(int viewType, ViewDataBinding viewDataBinding);

    /**
     * 设置无数据布局id
     *
     * @return
     */
    protected int getNoDataItemLayoutId() {
        return R.layout.item_no_data;
    }

    protected class NoDataViewHolder extends BaseViewHolder {

        private String mString;
        private int imgResId;

        public NoDataViewHolder(ViewDataBinding itemViewDataBinding) {
            super(itemViewDataBinding);
        }

        public NoDataViewHolder(ViewDataBinding itemViewDataBinding, String string, int imgResId) {
            super(itemViewDataBinding);
            mString = string;
            this.imgResId = imgResId;
        }

        @Override
        protected void bindingData(Object o, int position) {
            View img = itemView.findViewById(R.id.img_show);
            if (img instanceof ImageView && imgResId > 0) {
                ((ImageView) img).setImageResource(imgResId);
            }

            View text = itemView.findViewById(R.id.tv_show);
            if (text instanceof TextView && mString != null) {
                ((TextView) text).setText(mString);
            }
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (position == 0 && ((mItems.size() == 1 && mItems.get(0) == null) || mItems.size() == 0)) {
            return NO_DATA_ITEM_VIEW_TYPE;
        }
        return super.getItemViewType(position);
    }

    protected NoDataViewHolder emptyNoDataViewHolder(ViewDataBinding itemViewDataBinding) {
        return new NoDataViewHolder(itemViewDataBinding);
    }

    protected NoDataViewHolder emptyNoDataViewHolder(ViewDataBinding itemViewDataBinding, int resId, String text) {
        return new NoDataViewHolder(itemViewDataBinding, text, resId);
    }

}
